package com.atguigu.gulimall.order.listener;

import com.atguigu.gulimall.order.service.OrderService;
import com.atguigu.gulimall.order.vo.PayAsyncVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
public class OrderPayedListener {
    @Autowired
    OrderService orderService;

    /**
     * 接收支付宝异步通知,只要收到支付宝的异步通知，告诉我们订单支付成功，我们返回success，支付宝就再也不通知
     * 需要配置：
     * 1、登录拦截器需要设置放行
     * 2、本地环境使用内网穿透，接收通知时配置的域名与网关配置的不一致而无法正确转发到服务，需要在nginx修改请求头
     * nginx添加以下配置
     * server{
     *     server_name 内网穿透软件的代理地址; #已配置的基础上增加
     *     #新增代理配置，将/payed/notify的请求转发到 order.gulimall.com
     *     location /payed/notify {
     *          proxy_set_header Host order.gulimall.com
     *          proxy_pass http://gulimall;
     *     }
     * }
     */
    @PostMapping("/payed/notify")
    public String handleAlipayed(PayAsyncVO vo ,HttpServletRequest request) {
        //
        Map<String, String[]> map = request.getParameterMap();
        System.out.println("支付宝通知到位了。。。数据："+map);

        String result = orderService.handlePayResult(vo);
        return "success";
    }
}
